﻿package cn.antscript.airCMS.display.component 
{
	import cn.antscript.airCMS.data.Config;
	import cn.antscript.airCMS.data.FilesData;
	import cn.antscript.airCMS.data.IGroupBtn;
	import cn.antscript.airCMS.data.ImgsData;
	import cn.antscript.airCMS.data.MusicManager;
	import cn.antscript.airCMS.data.MusicsData;
	import cn.antscript.airCMS.data.singleData.AbstractData;
	import cn.antscript.airCMS.data.singleData.FileData;
	import cn.antscript.airCMS.data.singleData.ImgData;
	import cn.antscript.airCMS.data.singleData.MusicData;
	import cn.antscript.airCMS.data.singleData.TextData;
	import cn.antscript.airCMS.data.singleData.VideoData;
	import cn.antscript.airCMS.data.TextsData;
	import cn.antscript.airCMS.data.VideosData;
	import cn.antscript.airCMS.display.component.btnbase.AbstractIco;
	import cn.antscript.airCMS.display.component.btnbase.IcoBitmapdatas;
	import cn.antscript.model.SqlModel;
	import com.bit101.components.InputText;
	import com.bit101.components.Label;
	import com.bit101.components.RadioButton;
	import com.bit101.components.Text;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.utils.getDefinitionByName;
	
	/**
	 * ...
	 * @author 
	 */
	public class AddPanel extends Sprite
	{
		public static const ADD_COM:String = "addCom";
		
		
		public static var imgFilter:Array = [new FileFilter("图片", "*.jpg;*.gif;*.png")];
		public static var musicFilter:Array = [new FileFilter("音乐", "*.mp3;*.wav")];
		public static var videoFilter:Array = [new FileFilter("视频", "*.mp4;*.flv")];
		
		private var _groupLabel:RadioButton;
		private var _groupTxt:Label;
		private var _newLable:RadioButton;
		private var _input:InputText;
		
		private var _alert:Label;
		
		private var _label:Label;
		private var _labelTxt:InputText;
		
		private var _content:Label;
		private var _contentTxt:Text;		
		private static var _instance:AddPanel;
		
		
		private var _submitBtn:AbstractIco;
		private var _cancelBtn:AbstractIco;
		
		private var _data:IGroupBtn;
		private var _pageID:String;
		
		public var absData:Object;
		
		
        private var _file:FileReference;


		public var isNewGroup:Boolean;
		
		public function AddPanel(privateClass:PrivateClass) 
		{
			_alert = new Label(this, 60, 270, "");
			
			
			_groupLabel = new RadioButton(this, 60, -40, "所属分类:", true);
			_groupTxt = new Label(this, 140, -45);
			_newLable = new RadioButton(this, 300, -40, "新建分类:");
			_newLable.alpha = 0.5;
			_input = new InputText(this, 380, -40);
			_input.setSize(120, 20);
			_input.visible = false;
			_groupLabel.addEventListener(MouseEvent.CLICK, onSelect);
			_newLable.addEventListener(MouseEvent.CLICK, onSelect);
			
			
			_label = new Label(this, 0, 0, "标题");
			_labelTxt = new InputText(this, 60, 0, "");
			_labelTxt.setSize(440, 25);
			_labelTxt.addEventListener(Event.CHANGE, onChange);
			
			_content = new Label(this, 0, 40, "内容");
			_contentTxt = new Text(this, 60, 40, "");
			_contentTxt.addEventListener(Event.CHANGE, onChange);
			
			_submitBtn = new AbstractIco(IcoBitmapdatas.Save, "保存", this, 480, 280);
			_cancelBtn = new AbstractIco(IcoBitmapdatas.Delete, "返回", this, 480, 30);
			
			_submitBtn.addEventListener(MouseEvent.CLICK, onSubmit);
			_cancelBtn.addEventListener(MouseEvent.CLICK, onCancel);
			
		}
		
		private function onSelect(e:MouseEvent):void 
		{
			if (_groupLabel.selected) {
				_input.visible = false;
				_groupTxt.alpha = 1;
				_groupLabel.alpha = 1;
				_newLable.alpha = 0.5;
			}
			else {
				_input.visible = true;
				stage.focus = _input.textField;
				_groupTxt.alpha = 0.5;
				_groupLabel.alpha = 0.5;
				_newLable.alpha = 1;
			}
			isNewGroup = _newLable.selected;
		}
		
		private function onChange(e:Event):void 
		{
			_alert.text = "";
		}
		
		private function onSubmit(e:MouseEvent):void 
		{
			if(_data is FilesData) {
				absData = new FileData("", "", "", "", "");
			}
			else if(_data is ImgsData) {
				absData = new ImgData("", "", "", "", "","");
			}
			else if(_data is MusicsData) {
				absData = new MusicData("", "", "", "", "");
			}
			else if(_data is TextsData) {
				absData = new TextData("", "", "", "");
			}
			else if(_data is VideosData) {
				absData = new VideoData("", "", "", "", "");
			}
			else {
				return;
			}
			
			
			absData.label = _labelTxt.text;
			absData.txt = _contentTxt.text;
			WaitBar.getInstance().show(this.stage);
			SqlModel.getInstance().add(absData as AbstractData, _groupLabel.selected?_data["label"]:_input.text,_pageID, added);
		}
		public function added(id:String="-1"):void {
			
			absData.id = id;
			
			WaitBar.getInstance().hide();
			
			if (absData is TextData) {
				AlertPanel.getInstance().show("操作已完成", this.stage);
				hide();
				setData();
				dispatchEvent(new Event(ADD_COM));
				MusicManager.getInstance().correct();
			}
			else {
				_alert.text = "添加成功，请上传文件！";				
				upload();
				MusicManager.getInstance().correct();
			}
		}
		private function setData():void {
			if (_newLable.selected) {
				var tempData:Object;
				if (_data is FilesData) {
					tempData = new FilesData(XML(	"<files label='" + 
													_input.text + 
													"' ><file label='" +	absData.label +
													"' content='" +	absData.txt +
													"' url='" + absData["url"] +
													"' /></files>"));
					absData = tempData;
				}
				else if (_data is ImgsData) {
					tempData = new ImgsData(XML(	"<imgs label='" +
													_input.text + 
													"' ><img label='" +	absData.label +
													"' content='" +	absData.txt +
													"' url='" + absData["url"] +
													"' thumb='" + absData["thumb"] +
													"' /></imgs>"));
					absData = tempData;
				}
				else if(_data is MusicsData) {
					tempData = new MusicsData(XML(	"<musics label='" + 
													_input.text + 
													"' ><music label='" +	absData.label +
													"' content='" +	absData.txt +
													"' url='" + absData["url"] +
													"' /></musics>"));
					absData = tempData;
				}
				else if(_data is TextsData) {
					tempData = new TextsData(XML(	"<texts label='" + 
													_input.text + 
													"' ><text label='" +	absData.label +
													"' content='" +	absData.txt +
													"' /></texts>"));
					absData = tempData;
				}
				else if(_data is VideosData) {
					tempData = new VideosData(XML(	"<videos label='" + 
													_input.text + 
													"' ><video label='" +	absData.label +
													"' content='" +	absData.txt +
													"' url='" + absData["url"] +
													"' /></videos>"));
					absData = tempData;
				}
				else {
					return;
				}
			}
		}
		
		
		private function upload():void
		{
			
			_file = new FileReference();
			if (absData is ImgData) {
				_file.browse(imgFilter);
			}
			else if(absData is MusicData) {
				_file.browse(musicFilter);
			}
			else if(absData is VideoData) {
				_file.browse(videoFilter);
			}
			else if(absData is FileData) {
				_file.browse();
			}
			_file.addEventListener(Event.SELECT, onSelectd);
			_file.addEventListener(Event.CANCEL, onCancelUpload);
		}
		
		private function onSelectd(e:Event):void 
		{
			_file.removeEventListener(Event.SELECT, onSelectd);
			_file.removeEventListener(Event.CANCEL, onCancelUpload);
			
			if (absData is ImgData) {
				_file.upload(new URLRequest(Config.IMG_UPLOAD_URL));
			}
			else {
				_file.upload(new URLRequest(Config.FILE_UPLOAD_URL));
			}
			_file.addEventListener(ProgressEvent.PROGRESS, onUploadPro);
			_file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, onUploadCom);
			_file.addEventListener(IOErrorEvent.IO_ERROR, onUploadErr);
			
			ProgressPanel.getInstance().show(this.stage);
			
		}		
		
		private function onUploadCom(e:DataEvent):void 
		{
			_file.removeEventListener(ProgressEvent.PROGRESS, onUploadPro);
			_file.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA, onUploadCom);
			_file.removeEventListener(IOErrorEvent.IO_ERROR, onUploadErr);
			
			ProgressPanel.getInstance().hide();
			
			var xml:XML = XML(e.data);
			if (absData is ImgData) {
				(absData as ImgData).url = String(xml.@path);
				(absData as ImgData).thumb = String(xml.@thumb);
			}
			else {
				absData["url"] = String(xml.@path);
			}
			
			SqlModel.getInstance().updateURL(absData as AbstractData,updateURLCom);
			
			WaitBar.getInstance().show(this.stage);
			
			_alert.text = "";
		}
		
		private function updateURLCom():void
		{
			WaitBar.getInstance().hide();
			
			AlertPanel.getInstance().show("操作已完成", this.stage);
			hide();
			setData();
			dispatchEvent(new Event(ADD_COM));
			
			MusicManager.getInstance().correct();
		}		
		private function onUploadPro(e:ProgressEvent):void 
		{
			ProgressPanel.getInstance().setPro(Number(e.bytesLoaded / e.bytesTotal));
		}			
		private function onUploadErr(e:IOErrorEvent):void 
		{
			_file.removeEventListener(ProgressEvent.PROGRESS, onUploadPro);
			_file.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA, onUploadCom);
			_file.removeEventListener(IOErrorEvent.IO_ERROR, onUploadErr);
			
			MusicManager.getInstance().incorrect();
		}
		private function onCancelUpload(e:Event):void 
		{
			_file.removeEventListener(Event.SELECT, onSelectd);
			_file.removeEventListener(Event.CANCEL, onCancelUpload);
		}
		
		private function onCancel(e:MouseEvent=null):void 
		{			
			SurePanel.getInstance().show("还未保存，确定返回？",this.stage,hide);
		}		
		private function hide():void{
			this.visible = false;
		}
		
		public static function getInstance():AddPanel {
			if (_instance == null) {
				_instance = new AddPanel(new PrivateClass);
			}
			return _instance;
		}
		public function replace(absData_:AbstractData, data:IGroupBtn, pageID:String, container:DisplayObjectContainer = null):void {
			absData = absData_;
			_data = data;
			_pageID = pageID;
			
			_groupLabel.selected = true;
			isNewGroup = false;
			_input.visible = false;
			_groupTxt.alpha = 1;
			_groupLabel.alpha = 1;
			_newLable.visible = false;
			
			_labelTxt.text = absData["label"];
			_contentTxt.text = absData["txt"];
			_input.text = "";		
			
			if (container != null) {
				container.addChild(AddPanel.getInstance());
			}
			AddPanel.getInstance().x = container.stage.stageWidth / 2 - 280;
			AddPanel.getInstance().y = container.stage.stageWidth / 2 - 200;
			
			
			_cancelBtn.x = stage.stageWidth - 50 - AddPanel.getInstance().x;
			_cancelBtn.y = 50 - AddPanel.getInstance().y;
			
			this.visible = false;
			
			upload();
		}
		
		
		public function add(data:IGroupBtn, pageID:String,container:DisplayObjectContainer = null):void {
			_data = data;
			_pageID = pageID;
			this.visible = true;
			
			_groupLabel.selected = true;
			isNewGroup = false;
			_input.visible = false;
			_groupTxt.alpha = 1;
			_groupLabel.alpha = 1;
			_newLable.alpha = 0.5;
			_newLable.visible = true;
			
			_labelTxt.text = "";
			_contentTxt.text = "";
			_input.text = "";
			
			_groupTxt.text = data["label"];
			
			if (container != null) {
				container.addChild(AddPanel.getInstance());
			}
			AddPanel.getInstance().x = container.stage.stageWidth / 2 - 280;
			AddPanel.getInstance().y = container.stage.stageWidth / 2 - 200;
			
			
			_cancelBtn.x = stage.stageWidth - 50 - AddPanel.getInstance().x;
			_cancelBtn.y = 50 - AddPanel.getInstance().y;
			
			
			
			graphics.clear();
			graphics.beginFill(0x000000, 0.9);
			graphics.drawRect( -AddPanel.getInstance().x, -AddPanel.getInstance().y, container.stage.stageWidth, container.stage.stageHeight);
			graphics.endFill();
			
			
			
		}
		
		public function get label():String {
			return _labelTxt.text;
		}
		public function get content():String {
			return _contentTxt.text;
		}
		
	}

}

class PrivateClass {
	
}